package cn.txy.user.controller;

import cn.hutool.crypto.SecureUtil;
import cn.txy.config.R;
import cn.txy.user.domain.User;
import cn.txy.user.query.UserQuery;
import cn.txy.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation(value = "根据id查询用户信息")
    @GetMapping("/{id}")
    public R findById(@ApiParam @PathVariable("id")Long id){
        return R.ok(userService.findById(id));
    }
    @ApiOperation(value = "查询所有用户信息")
    @GetMapping
    public R findAll(){
        return R.ok(userService.findAll());
    }

    @ApiOperation(value = "根据id删除用户")
    @DeleteMapping("/{id}")
    public R deleteById(@ApiParam @PathVariable("id")Long id){
        userService.deleteById(id);
        return R.ok();
    }
    @ApiOperation(value = "批量删除用户信息")
    @PostMapping("/deleteAll")
    public R batchDelete(@RequestBody List<Long> ids){
        userService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation(value = "添加和修改用户信息")
    @PostMapping
    public R add(@RequestBody User user){
        if(user.getId() == null){
            userService.add(user);
        }else {
            userService.update(user);
        }
        SecureUtil.md5();
        return R.ok();
    }
    @ApiOperation(value = "分页查询用户信息")
    @PostMapping("/page")
    public R queryPage(@RequestBody UserQuery userQuery){
        return R.ok(userService.queryPage(userQuery));
    }

}
